package com.acknight.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.acknight.constant.SystemConstants;
import com.acknight.dto.Result;
import com.acknight.dto.ScrollResult;
import com.acknight.dto.UserDTO;
import com.acknight.entity.Blog;
import com.acknight.entity.User;
import com.acknight.mapper.BlogMapper;
import com.acknight.service.IBlogService;
import com.acknight.service.IUserService;
import com.acknight.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.acknight.constant.RedisConstants.BLOG_LIKED_KEY;
import static com.acknight.constant.RedisConstants.FEED_KEY;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Resource
    private IUserService userService;

    @Override
    public Result queryHotBlog(Integer current) {
        // 根据用户查询
        Page<Blog> page = query()
                .orderByDesc("liked")
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        // 查询用户
        records.forEach(blog -> {
            this.queryBlogUser(blog);
        });
        return Result.ok(records);
    }

    @Override
    public Result queryBlogById(Long id) {
        // 1.查询blog
        Blog blog = getById(id);
        if (blog == null || blog.getIsDelete().equals(1)) {
            return Result.fail("笔记不存在!");
        }

        // 2.查询blog有关的用户
        queryBlogUser(blog);

        return Result.ok(blog);
    }


    @Override
    public Result saveBlog(Blog blog) {
        // 1.获取登录用户
        UserDTO user = UserHolder.getUser();
        blog.setUserId(user.getId());

        // 2.保存探店博文
        boolean isSuccess = save(blog);
        if (!isSuccess) {
            return Result.fail("新增笔记失败!");
        }

        // 5.返回id
        return Result.ok(blog.getId());
    }

    @Override
    public Result getBlogByShopId(Long id) {
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Blog::getShopId, id);
        queryWrapper.eq(Blog::getIsDelete, 0);
        List<Blog> blogs = list(queryWrapper);
        blogs.forEach(blog -> {this.queryBlogUser(blog);});
        return Result.ok(blogs);
    }

    @Override
    public Result getBlogByUserId(Long id) {
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Blog::getUserId, id);
        queryWrapper.eq(Blog::getIsDelete, 0);
        List<Blog> blogs = list(queryWrapper);
        return Result.ok(blogs);
    }

    @Override
    public Result deleteBlogById(Long id) {
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Blog::getId, id);
        Blog result = getOne(queryWrapper);
        if (result == null) {
            return Result.fail("不存在该博客!");
        }
        result.setIsDelete(1);
        updateById(result);
        return Result.ok();
    }

    private void queryBlogUser(Blog blog) {
        Long userId = blog.getUserId();
        User user = userService.getById(userId);
        blog.setName(user.getNickName());
        blog.setIcon(user.getIcon());
    }
}
